<?php
namespace app\index\service;


use app\index\model\DeptModel;
use app\index\model\EnginBianModel;
use app\index\model\EnginModel;
use app\index\model\ZhiJieModel;
use app\index\model\UserModel;
use app\index\relevan\CertRelevan;
use think\Db;

class EnginService extends CommonService
{
    public function get_engin_state(){
        return [
            1=>'在投',
            3=>'中标',
            5=>'未中标',
            7=>'在建',
            9=>'竣工',
        ];
    }
    public function get_engin_state_name($state, $status){
        $name = '';
        if($status == 1){
            $arr = $this->get_engin_state();
            $name = isset($arr[$state]) ? $arr[$state] : '';
        }
        return $name;
    }

    public function check_engin_name($name, $id=0){
        return !!EnginModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0,'isturn'=>1])
            ->where('status','in', [0,1])->where('id','neq', $id)->find();
    }

    public function set_engin_state($enginid){
        $state = 0;
        $engininfo = EnginModel::where(['id'=>$enginid])->find();
        if($engininfo['isturn'] == 1 || $engininfo['status'] == 1){
            if(!$engininfo['kaiguo']){
                $state = 1;
            }else{
                if($engininfo['kaiguo'] == '中标'){
                    if(!$engininfo['shikaidt'] || $engininfo['shikaidt'] == '0000-00-00'){
                        $state = 3;
                    }else{
                        if(!$engininfo['shijundt'] || $engininfo['shijundt'] == '0000-00-00'){
                            $state = 7;
                        }else{
                            $state = 9;
                        }
                    }
                }else{
                    $state = 5;
                }
            }
        }

        EnginModel::where(['id'=>$engininfo['id']])->update(['state'=>$state]);

        (new CertRelevan())->set_cert_statueuse_engin($engininfo['id']);
        return true;
    }
    public function set_engin_bianer($enginid){
        $engininfo = EnginModel::where(['id'=>$enginid])->find();
        EnginBianModel::where(['enginid'=>$engininfo['id']])->delete();

        if($engininfo['isturn'] == 1 || $engininfo['status'] == 1){
            $bianerArr = explode(',', $engininfo['bianer']);
            foreach ($bianerArr as $bianer){
                $ins = [];
                $ins['enginid'] = $engininfo['id'];
                $ins['bianer'] = $bianer;
                EnginBianModel::insert($ins);
            }

        }
        return true;
    }
    public function get_engin_showinfo($enginid){
        $engininfo = EnginModel::where(['id'=>$enginid])->find();
        if(!$engininfo){
            return [];
        }
        $engininfo['state_name'] = (new EnginService())->get_engin_state_name($engininfo['state'], $engininfo['status']);
        $engininfo['business_name'] = $engininfo['business'] == 1 ? '自营' : '联营';
        $engininfo['chanid_name'] = DeptModel::get_name_by_pk($engininfo['chanid']);
        $engininfo['man_xiang_name'] = UserModel::get_realname_by_pk($engininfo['man_xiang']);
        $engininfo['myjieyufee'] = ZhiJieModel::where(['enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'created_user'=>$this->userinfo['id']])->sum('yufee');
        return $engininfo;
    }
    public function get_user_engin(){
        $str = "find_in_set(".$this->userinfo['id'].",man_zhuan)";
        $str .= " or find_in_set(".$this->userinfo['id'].",man_zi)";
        $str .= " or find_in_set(".$this->userinfo['id'].",man_cheng)";
        $str .= " or find_in_set(".$this->userinfo['id'].",man_zhian)";
        $where = Db::raw($str);
        $arr = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where($where)
            ->column('id');
        return $arr;
    }

    public function get_engin_budget_budtype(){
        return [
            'cai'=>'材料',
            'ren'=>'人工',
            'ji'=>'机械设备',
            'fen'=>'专业分包',
            'qi'=>'其他',
        ];
    }
    public function get_engin_budget_budtype_name($budtype){
        $arr = $this->get_engin_budget_budtype();
        return isset($arr[$budtype]) ? $arr[$budtype] : '';
    }
    public function get_engin_budget_type_name($type){
        $str = '';
        if($type == 1){
            $str = '调整';
        }elseif($type == 2){
            $str = '增项';
        }elseif($type == 3){
            $str = '调整及增项';
        }
        return $str;
    }

    public function get_engin_fold_table(){
        return [
            'engin' => '项目信息',
            'shou_tou' => '投标费收取',
            'zhi_tou' => '投标费支付',

            'engin_budget' => '预算编制',
            'contract_shi' => '收入合同',
            'contract_bu' => '收入合同补充合同',
            'engin_visa' => '项目签证',
            'engin_chan' => '产值月报',
            'contract_shi_jie' => '收入合同结算',
            'piao_kai' => '项目开票',
            'shou_xiang' => '项目收款',

            'contract_cai' => '支出合同',
            'contract_bian' => '支出合同变更',
            'contract_cai_jie' => '支出合同结算',
            'contract_buy' => '采购单',
            'piao_shou' => '项目收票',
            'zhi_xiang' => '项目付款',
            'zhi_enbao' => '项目费用报销',

            'contract_lian' => '联营合同',
            'zhi_jintui' => '进项退税',
            'zhi_lianjie' => '联营结算',

            'stuff_ru' => '材料入库',
            'device_jin' => '设备进场',
            'labour_gong' => '工人花名册',
            'labour_xiang' => '项目花名册',
            'labour_yong' => '用工单',

            'engin_godan' => '工程量清单',
            'speed_zong' => '总进度计划',
            'speed_month' => '月进度计划',
            'engin_diary' => '施工日志',
            'engin_speed' => '进度填报',
            'engin_xun' => '项目巡检',
            'engin_zheng' => '整改通知',
        ];
    }


















}
